home *** CD-ROM | disk | FTP | other *** search
/ Fritz: All Fritz / All Fritz.zip / All Fritz / FILES / PROGNG_C / DBTOOLC.LZH / SOURCE.ARC / GETPASS.C < prev    next >
Text File  |  1986-06-10  |  3KB  |  114 lines

  1. /* function to get a password from the keyboard */
  2. getpass(maxchar,ret1,bell,del,retint,autret,maskchar)
  3. int maxchar; /* max char(s) to accept in the password */
  4. int ret1; /* return keys to signal end of input. Summed values       */
  5.           /* of keys you wish to use as the signal. These chars      */
  6.           /* are not echoed.                                         */
  7.           /* 1024 - cursor down, 512 - cursor up, 256 - cursor right */
  8.           /* 128 - cursor left, 64 - end, 32 - home, 16 - shift tab  */
  9.           /* 8 - tab, 4 - PgDn, 2 - PgUp, 1 - <cr>                   */
  10. int bell; /* 0 - no bell, anything else,yes a bell for wrong input   */
  11. int del; /* delete char(s) to accept 0 - backspace key               */
  12.          /*                          1 - delete key                  */
  13.          /*  anything else use both keys                             */
  14. char *retint; /* pointer to where to return password */
  15. int autret; /* 0 - automatic return when hit maxchar */
  16.             /* anything else, no automatic return    */
  17. char maskchar; /* character to use as a mask character */
  18. {
  19. char ve[11][2],c;
  20. int i,j,k,l,ret;
  21.  
  22. if((maxchar < 1)||(maxchar > 2000)) return(-1); /* maxchar out of range */
  23. i = ret1; retint[0] = '\0';
  24. for(k = 1024,j = 0; j < 11; ++j,k=k/2)
  25.   {
  26.   if(i >= k)
  27.     {
  28.     i = i - k;
  29.     if((j == 7)||(j == 10)) ve[j][0] = 0;
  30.       else ve[j][0] = 1;
  31.     switch(j)
  32.       {
  33.       case 0: ve[0][1] = 80; break;
  34.       case 1: ve[1][1] = 72; break;
  35.       case 2: ve[2][1] = 77; break;
  36.       case 3: ve[3][1] = 75; break;
  37.       case 4: ve[4][1] = 79; break;
  38.       case 5: ve[5][1] = 71; break;
  39.       case 6: ve[6][1] = 15; break;
  40.       case 7: ve[7][1] = 9 ; break;
  41.       case 8: ve[8][1] = 81; break;
  42.       case 9: ve[9][1] = 73; break;
  43.       case 10:ve[10][1] = 13; break;
  44.       }
  45.     }
  46.     else
  47.     {
  48.     ve[j][0] = -1; ve[j][1] = -1;
  49.     }
  50.   }
  51. i = 0; ret = 0;
  52. for( ; ; )
  53.   {
  54.   k = getkey(&j);
  55.   /* one of the end input keys */
  56.   for(l = 0; l < 11; ++l)
  57.     {
  58.     if((k == ve[l][0])&&(j == ve[l][1]))
  59.       {
  60.       ret = ve[l][1];
  61.       return(ret);
  62.       }
  63.     }
  64.   /* delete or backspace key */
  65.   c = 'b';
  66.   if((k == 0)&&(j == 8))
  67.     {
  68.     if((del != 1)&&(i != 0)&&(del != -1)) c = 'y';
  69.     }
  70.   if((k == 1)&&(j == 83))
  71.     {
  72.     if((del != 0)&&(i != 0)&&(del != -1)) c = 'y';
  73.     }
  74.   if(k == 0)
  75.     {
  76.     if((j > 96)&&(j < 123)) c = 'n';
  77.     if((j > 64)&&(j < 91)) c = 'n';
  78.     if((j > 47)&&(j < 58)) c = 'n';
  79.     }
  80.   if(c == 'y')
  81.     {
  82.     curback();
  83.     wrtch(1,' ');
  84.     --i;
  85.     retint[i] = '\0';
  86.     continue;
  87.     }
  88.     else
  89.     {
  90.     if(c == 'b')
  91.       {
  92.       if(bell != 0) 
  93.           sound(500,2);
  94.       continue;
  95.       }
  96.     }
  97.   if(i == maxchar)
  98.     {
  99.     if(bell != 0) sound(500,2);
  100.     continue;
  101.     }
  102.   /* char is valid */
  103.   retint[i] = j;
  104.   ++i;
  105.   retint[i] = '\0';
  106.   wrtch(1,maskchar);
  107.   curfwd();
  108.   if((autret == 0)&&(i == maxchar))
  109.     {
  110.     return(-2);
  111.     }
  112.   }
  113. }
  114.